package org.me.Anime;

import java.util.List;

import org.me.Anime.Utli.Algorithm;
import org.me.Anime.Utli.BitmapUtli;
import org.me.Anime.Utli.FileUtli;
import org.me.Anime.Utli.GlobalVariable;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class DrawAnimeView extends View {
	
	private Bitmap mBitmap;
	Bitmap mBitmapConvert;

    Bitmap photoBitmap;
	
	private Paint mPaint;
	private Path mPath;
	private ScaleGestureDetector mScaleGestureDetector;
	public static float mScale = 100.0f;
	private List<Path> mPaths;
	Canvas mCanvas;
	Context mContext;
	
	
	public DrawAnimeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init(context);
	}

	public DrawAnimeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init(context);
	}

	public DrawAnimeView(Context context) {
		super(context);
		mContext = context;
		init(context);
	}
	
	private void init(Context context) {

			mPaint = new Paint();
			mPaint.setFilterBitmap(true);
			mPaint.setAntiAlias(true);
			mPaint.setColor(GlobalVariable.COLOR);
			mPaint.setStrokeWidth(5.0f);
			mPaint.setStyle(Style.STROKE);
			mPaint.setTextSize(36.0f);
			
			//new loadImageSyncTask().execute();

			GlobalVariable.byteBuffer = FileUtli.readFileFromAssets(mContext, GlobalVariable.fileName);
			photoBitmap	= BitmapUtli.ConvertBitmap(GlobalVariable.byteBuffer);
			mBitmapConvert = BitmapUtli.ConvertImageMutable(photoBitmap);
			
		
		mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener(){
			@Override
			public boolean onScale(ScaleGestureDetector detector) {
				//mScale*=detector.getScaleFactor();
				mPath = null;
				invalidate();
				return true;
			}
		});
		
	}
	
	private void DrawBackground(Canvas canvas)
	{
		canvas.drawColor(Color.WHITE);
	}
	
	private void DrawImageFill(Canvas canvas)
	{
		if(!GlobalVariable.isDelete)
		canvas.drawBitmap(mBitmapConvert, 0, 0, mPaint);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		Log.e("onDraw", "GlobalVariable.COLOR = "+GlobalVariable.COLOR);
		DrawBackground(canvas);
		DrawImageFill(canvas);
			
		if(mBitmap!=null){
			canvas.drawBitmap(mBitmap, 0, 0, mPaint);
		}
		
		if(mPaths!=null){
			for(int i = 0; i<mPaths.size();i++){
				canvas.drawPath(mPaths.get(i), mPaint);
			}
		}
		
		if(mPath!=null){
			canvas.drawPath(mPath, mPaint);
		}
		canvas.restore();
		
	}

	public Bitmap getBitmap() {
		return mBitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		mBitmap = bitmap;
		invalidate();
	}
	
	
	private void touch_start(float x, float y)
	{
		mPath = new Path();
		mPath.moveTo(x, y);
		
    }
    private void touch_move(float x, float y) 
    {
    	if(mPath!=null){
			mPath.lineTo(x, y);
		}
    }
    private void touch_up(float x, float y) 
    {
    	if(mPath!=null && mPaths!=null){
			mPath.lineTo(x, y);
			mPaths.add(mPath);
			mPath = null;
		}
    }
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		mScaleGestureDetector.onTouchEvent(event);
		if(mScaleGestureDetector.isInProgress()){
			return true;
		}
		 Point pnt =new Point((int)event.getX(),(int)event.getY());
		
		switch(event.getActionMasked()){
		case MotionEvent.ACTION_DOWN:
			if(!GlobalVariable.IsDraw)
			{
				GlobalVariable.targetColor = mBitmapConvert.getPixel((int)event.getX(),(int)event.getY());
				Algorithm.FloodFill(mBitmapConvert, pnt, GlobalVariable.targetColor, GlobalVariable.COLOR1);
			}
			else
				touch_start(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_MOVE:
			if(GlobalVariable.IsDraw)
				touch_move(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_UP:
			if(GlobalVariable.IsDraw)
				touch_up(event.getX(), event.getY());
			break;
		}
		invalidate();
		return true;
	}

	public Paint getPaint() {
		return mPaint;
	}

	public void setPaint(Paint paint) {
		mPaint = paint;
	}

	public List<Path> getPaths() {
		return mPaths;
	}

	public void setPaths(List<Path> paths) {
		mPaths = paths;
	}
	
	//Task draw image
	
	private class loadImageSyncTask extends AsyncTask<String, Integer, Integer>
	{
		private ProgressDialog mProgressDialog;
		
		@Override
		protected void onPreExecute()
		{
			mProgressDialog = new ProgressDialog(mContext);
			mProgressDialog.setTitle("Loading");
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.show();
		}

		@Override
		protected Integer doInBackground(String... params)
		{
			
			GlobalVariable.byteBuffer = FileUtli.readFileFromAssets(mContext, GlobalVariable.fileName);
			photoBitmap	= BitmapUtli.ConvertBitmap(GlobalVariable.byteBuffer);
			mBitmapConvert = BitmapUtli.ConvertImageMutable(photoBitmap);
			return 1;
		}		
				
		@Override
		protected void onPostExecute(Integer result)
		{
			
			mProgressDialog.cancel();
			
			
		}
	}

}
